﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using HROReporting.Infrastructure.Authentication;
using HROReporting.Services.Interfaces;
using HROReporting.Controllers.ActionArguments;
using HROReporting.Controllers.ViewModels.Account;

namespace HROReporting.Controllers.Controllers
{
    public class AccountLogOnController : BaseAccountController
    {
        public AccountLogOnController(ILocalAuthenticationService authenticationSerivice,
                                      IStudentService studentService,
                                      IFormsAuthentication formsAuthentication,
                                      IActionArguments actionArguments)
            : base(authenticationSerivice,
                   studentService,
                   formsAuthentication,
                   actionArguments)
        {
        }

        public ActionResult LogOn()
        {
            if (Request.IsAuthenticated)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                AccountView accountView = InitializeAccountViewWithIssue(false, "");

                return View(accountView);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult LogOn(string email, string password)
        {
            User user = _authenticationService.Login(email, password);

            if (user.IsAuthenticated)
            {
                _formsAuthentication.SetAuthenticationToken(email);

                return RedirectToAction("Index", "Home");
            }
            else
            {
                AccountView accountView = InitializeAccountViewWithIssue(true, "We kunnen u helaas niet inloggen. " + "Probeer het nog eens.");

                return View("LogOn", accountView);
            }
        }

        public ActionResult SignOut()
        {
            _formsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

        private AccountView InitializeAccountViewWithIssue(bool hasIssue, string message)
        {
            AccountView accountView = new AccountView();
            accountView.HasIssue = hasIssue;
            accountView.Message = message;

            return accountView;
        }
    }
}
